package com.wd.tech.activity.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.stx.xhb.xbanner.XBanner;
import com.wd.tech.R;
import com.wd.tech.activity.activity.FlowLayoutActivity;
import com.wd.tech.activity.activity.HomePageForDetailsActivity;
import com.wd.tech.activity.activity.MoreActivity;
import com.wd.tech.activity.adaptert.RecommendationDisplayAdaptert;
import com.wd.tech.activity.base.BaseFragment;
import com.wd.tech.activity.base.BasePreantert;
import com.wd.tech.activity.bean.BeannerBean;
import com.wd.tech.activity.bean.RecommendationDisplayBean;
import com.wd.tech.activity.contreantert.Contreatnert;
import com.wd.tech.activity.preantenrt.Preantert;
import com.wd.tech.activity.utils.NetUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author 时文豪
 * @description:
 * @date :2020/7/2 12:00
 */
public class ConsultingServiceFragment extends BaseFragment implements Contreatnert.IView {
    @BindView(R.id.consulting_banner)
    XBanner xb;
    @BindView(R.id.consulting_rv)
    XRecyclerView rv;
    int i = 1;
    private List<RecommendationDisplayBean.ResultBean> result;
    private RecommendationDisplayAdaptert recommendationDisplayAdaptert;

    @Override
    protected int getLayoutId() {
        return R.layout.consultingservice;
    }

    @Override
    protected BasePreantert initPreantert() {
        return new Preantert(this);

    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {
        BasePreantert basePreantert = getmPreantert();
        if (basePreantert instanceof Contreatnert.IPreantert) {
            ((Preantert) basePreantert).OnSuccess();
        }
        rv.setPullRefreshEnabled(true);
        rv.setLoadingMoreEnabled(true);
        rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                i = 1;
                rv.refreshComplete();
            }

            @Override
            public void onLoadMore() {

                recommendationDisplayAdaptert.notifyDataSetChanged();
                i++;
                rv.refreshComplete();
            }
        });
        //未使用MVP直接调取i成功方法
        NetUtils.getInstance().getApis().getPlateBean(i, 1, 10)

                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RecommendationDisplayBean>() {


                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RecommendationDisplayBean plateBean) {
                        result = plateBean.getResult();
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                        rv.setLayoutManager(linearLayoutManager);
                        recommendationDisplayAdaptert = new RecommendationDisplayAdaptert(getActivity(), result);
                        recommendationDisplayAdaptert.onClick(new RecommendationDisplayAdaptert.setOnClicked() {
                            @Override
                            public void OnClick(RecommendationDisplayBean.ResultBean resultBean) {
                                Intent intent = new Intent(getActivity(), HomePageForDetailsActivity.class);
                                int id = resultBean.getId();
                                String format = new SimpleDateFormat("yy_MM-dd").format(new Date(resultBean.getReleaseTime()));
                                String title = resultBean.getTitle();
                                String source = resultBean.getSource();
                                intent.putExtra("home_id", id);
                                intent.putExtra("title", title);
                                intent.putExtra("source", source);
                                intent.putExtra("format", format);
                                Log.i("xxxx", id + "");

                                startActivity(intent);
                            }
                        });
                        rv.setAdapter(recommendationDisplayAdaptert);

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //点击图片进行跳转到更多的Activity
    @OnClick({R.id.more})
    public void more() {
        startActivity(new Intent(getContext(), MoreActivity.class));
    }

    //点击按钮进行跳转搜索
    @OnClick(R.id.search)
    public void search() {
        startActivity(new Intent(getContext(), FlowLayoutActivity.class));
    }


    @Override
    public void BannerSuccess(BeannerBean bean) {
        List<BeannerBean.ResultBean> result = bean.getResult();
        xb.setBannerData(result);
        xb.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                String imageUrl = result.get(position).getImageUrl();
                Glide.with(getContext())
                        .load(imageUrl)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(((ImageView) view));


            }
        });
    }
}
